<template>
  <s-modal
    title="修改密码"
    :visible="visible"
    width="400px"
    ok-text="修改"
    :confirm-loading="confirmLoading"
    :closable="!confirmLoading"
    @ok="save()"
    @cancel="
      () => {
        if (!confirmLoading) {
          visible = false
        }
      }
    "
  >
    <div>
      <s-scroll>
        <s-form ref="form" :value="formValue">
          <s-form-item
            label="密码"
            size="big"
            name="password"
            :rules="[
              { whitespace: true, required: true, message: '密码不能为空' },
              { max: 50, message: '最长为50' }
            ]"
          >
            <s-input v-model="formValue.password" allow-clear placeholder="密码" />
          </s-form-item>
        </s-form>
      </s-scroll>
    </div>
  </s-modal>
</template>

<script>
export default {
  name: 'UpdatePassword',
  data() {
    return {
      formValue: {},
      confirmLoading: false,
      visible: false
    }
  },
  methods: {
    open(id) {
      this.formValue = {}
      this.formValue.id = id
      this.visible = true
    },
    save() {
      const form = this.$refs.form
      form.validate((values) => {
        values.id = this.formValue.id
        this.confirmLoading = true
        this.$api
          .PUT('/realm/user/changePassword', values)
          .then((res) => {
            this.confirmLoading = false
            this.visible = false
            this.$message.info(res.msg)
          })
          .catch(() => {
            this.confirmLoading = false
          })
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
